package com.controller;

import com.pojo.User;
import com.service.UserService;
import com.utils.JSONResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户登陆
 *
 * @author lxw
 * @create 2017-08-17 14:22
 **/
@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     *查看用户的权限和角色
     * */
    @RequestMapping(value = "/tolist",method =  RequestMethod.GET)
    @ResponseBody
    public JSONResult AllUrls(){
        List<User> list = userService.findUrl("张三");
        return new JSONResult(list);
    }


    /**
    *查看用户的权限和角色
    * */
    @RequestMapping(value = "/list",method =  RequestMethod.GET)
    @ResponseBody
    public JSONResult showAllUrls(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        if(user.getUsername().equals("admin")){
            List<User> list = userService.findAllUrl();
            return new JSONResult(list);
        }else{
            List<User> list = userService.findUrl(user.getUsername());
            return new JSONResult(list);
        }
    }

    /**
     * 跳转到登陆页面
     * */
    @RequestMapping(value="/toLogin")
    public ModelAndView toLogin(){
        ModelAndView modelAndView = new ModelAndView("login");
        return modelAndView;
    }

    /**
    * 登陆信息
    * */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(HttpServletRequest request, HttpSession session){
        //获取前台的账号密码
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");

        int i=0;
//      用户账户没有找到为空，null.equals(user)就会成立，相当于没有当前账户
       User users = userService.findUserByName(userName);
        if (users == null || !users.getPassword().equals(password)) {
            System.out.println("密码错误");
            return null;
        }
        //将登陆的信息存入session中
        session.setAttribute("user", users);
        //获取权限列表
        List<User> urls = userService.findUrl(userName);
       /* //将权限列表的对象找到url转化为String类型存入数组中
        List<String> list = new ArrayList<String>();
        for(User user :urls)
        {
            list.add(user.getUrl());
        }
        String[] arr=new String[urls.size()];
        list.toArray(arr);*/
        Map<Integer,String> umap = new HashMap<Integer,String>();
        for(User user:urls){
            umap.put(i++,user.getUrl());
        }
        session.setAttribute("urls", umap);
        return "success";
    }

    /**
     * 登出方法
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult logout(HttpSession session) {
        session.setAttribute("user", null);
        return new JSONResult();
    }

    /**
    * 跳转到添加用户界面
    * */
    @RequestMapping(value="/toAdd",method = RequestMethod.GET)
    public ModelAndView toAddUser(){
        ModelAndView modelAndView = new ModelAndView("list");
        return modelAndView;
    }

    /**
     * 更新权限
    * */
    @RequestMapping(value="/toUpdate")
    public ModelAndView toUpdate(){
        ModelAndView modelAndView = new ModelAndView("list");
        return modelAndView;
    }

    /**
    * 删除权限
    * */
    @RequestMapping(value="/toDel")
    public ModelAndView toDel(){
        ModelAndView modelAndView = new ModelAndView("list");
        return modelAndView;
    }

    /**
    * 查看权限
    * */
    @RequestMapping(value="/toFind", method = RequestMethod.GET)
    public ModelAndView toFind(){
        ModelAndView modelAndView = new ModelAndView("list");
        return modelAndView;
    }

    /**
    * 没有权限的失败界面
    * */
    @RequestMapping(value="/toFailed")
    public ModelAndView toFailed(){
        ModelAndView modelAndView = new ModelAndView("failed");
        return modelAndView;
    }
}
